<?php

/**
 * Created by Mauro Gadaleta
 * User: Mauro Gadaleta
 * Date: 28/04/11
 * Time: 12:22
 *
 * Clase Base Modelo
 */
class model {
    /**
     * @var 
     */
	private $connection;

    /**
     * @var
     */
    private $dirModel;

    /**
     * @var array
     */
	private $updateOptions = array(
		'packagesPrefix'		=>	'Package',
		'packagesPath'			=>	'',
		'packagesFolderName'	=>	'packages',
		'suffix'				=>	'.php',
		'generateBaseClasses'	=>	true,
		'generateTableClasses'	=>	false,
		'generateAccesors'		=>	false,
		'baseClassPrefix'		=>	'Base',
		'baseClassesDirectory'	=>	'base',
		'baseClassName'			=>	'Doctrine_Record'
	);

	/**
     * constructor set up
     */
	function __construct() {
        $this->setDirModels();
    }

    /**
     * @return void
     */
    private function setDirModels() {
        if(is_dir('src/models'))
            $this->dirModel = 'src/models';
        elseif(is_dir('../src/models'))
            $this->dirModel = '../src/models';
    }

    /**
     * Devuelve la ultima conexión
     * @return Doctrine_Connection
     */
	public function getLastConnection() { return Doctrine_Manager::connection(); }

    /**
     * Devuelve la conexión actual
     * @return Doctrine_Connection
     */
	public function getConnection() { return Doctrine_Manager::getCurrentConnection(); }

    /**
     * Genera los modelos desde la base de datos
     * @return void
     */
	public function generateModels() {
        Doctrine_Core::generateModelsFromDb($this->dirModel,array('doctrine'),$this->updateOptions);
	}

    /**
     * Genera los modelos desde un archivo yaml
     * @param string $schemaYaml
     * @return void
     */
	public function generateModelsFromYaml($schemaYaml = 'schema.yml') {
		Doctrine_Core::generateModelsFromYaml($schemaYaml, 'models', $this->updateOptions);
	}

    /**
     * Genera la tabla de configuración y la tabla de usuarios
     * @param  $data
     * @return void
     */
    public function generateConfigurationTable($data) {
        Doctrine::createTablesFromArray(array('MFConfiguration','MFUser'));

        $con = Doctrine_Manager::connection();
        $tables = $con->import->listTables('MFConfiguration');

        if(is_int(array_search('configuration',$tables))) {
            Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh()->query('TRUNCATE TABLE configuration');
            Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh()->query('TRUNCATE TABLE users');
        }

        $this->generateModels();

        $user = new MFUser();
        $user->email = $data['cms_user_name'];
        $user->password = md5($data['cms_password']);
        $user->cms_access = true;
        $user->active = true;
        $user->registration_date = date('Y-m-d H:i:s');
        $user->token = utils::generateToken();
        $user->save();

        unset($data['cms_user_name']);
        unset($data['cms_password']);
        $configuration = new MFConfiguration();
        $configuration->setArray($data);
        $configuration->debug = true;
        $configuration->save();
    }
}

?>